<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>
<body>
  <div id="app">
    <common-head></common-head>
  
  </div>
  <script src="./vue.js"></script>
  <script>
    // 定义第三方实例
    const bus = new Vue();
    // 子组件1
    const Child = {
      template: `
        <div>
          <h3>这是子组件1</h3>  
          <button @click="emit">向我的兄弟传递数据</button>
        </div>
      `,
      data(){
        return {
          msg: '这是子组件1中的数据'
        }
      },
      methods: {
        emit(){
          // 兄弟组件1中 bus触发 bus的自定义事件
          bus.$emit('biubiubiu', this.msg)
        }
      }
    }
    // 子组件2
    const Child2 = {
      template: `
        <div>
          <h3>子组件2</h3>  
          {{msg}}
        </div>
      `,
      data(){
        return {
          msg: '这是兄弟组件2原来的值'
        }
      },
      mounted(){
        // 在这里监听
        
        bus.$on('biubiubiu', (msg) => {
          this.msg = msg;
        })
      }
    }
    // 父组件1
    const CommonHead = {
      template: `
        <div>
          <h2>这是一个公共的 头部组件</h2>  
          <hr/>
          <child></child>
          <br/>
          <child2></child2>
        </div>
      `,
      
      components: {
        Child,
        Child2
      }
    };
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app'
    })
    
  </script>
</body>
</html>